package com.amgl.mobike.module.search;

import com.amgl.mobike.mvp.BasePresenter;
import com.amgl.mobike.network.AppNetworkManager;
import com.amgl.mobike.network.api.SearchApi;
import com.amgl.mobike.network.response.ListResult;
import com.amgl.mobike.network.response.SearchResult;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import timber.log.Timber;

/**
 * Created by 阿木 on 2017/5/27.
 */

public class SearchPresenter extends BasePresenter<ISearchContract.ISearchView> implements ISearchContract.ISearchPresenter {
    public SearchPresenter(ISearchContract.ISearchView view) {
        super(view);
        mSearchApi = AppNetworkManager.getInstance().getSearchApi();
    }

    private SearchApi mSearchApi;

    private int mLastIndex;

    @Override
    public void search(String query) {
        Timber.d("start search: %s", query);
        final int startIndex = 0;

        mLastIndex = 0;

        Observable<String> observable = mSearchApi.search(query, startIndex, 50);
        Observer<String> observer = new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String s) {
                Timber.d("on next: %s", s);
                onResponseData(startIndex, s, false);
            }

            @Override
            public void onError(Throwable e) {
                Timber.w(e);
                onResponseError(e);
            }

            @Override
            public void onComplete() {

            }
        };

        doRequest(observable, observer);
    }

    @Override
    public void loadMore(String query) {
        final int startIndex = mLastIndex;
        Timber.d("start  search more: %s;  start: %s", query, startIndex);

        Observable<String> observable = mSearchApi.search(query, startIndex, 50);
        if (getView() != null) {
            getView().showLoadingMore(true);
        }

        Observer<String> observer = new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String s) {
                Timber.d("on next: %s", s);
                onResponseData(startIndex, s, true);
            }

            @Override
            public void onError(Throwable e) {
                Timber.w(e);
                onResponseError(e);
            }

            @Override
            public void onComplete() {

            }
        };

        doRequest(observable, observer);
    }

    private void onResponseData(int startIndex, String response, boolean isMore) {
        Timber.d("response: %s", response);
        Gson gson = new Gson();
        ListResult listResult = null;
        try {
            listResult = gson.fromJson(response, ListResult.class);
        } catch (JsonSyntaxException e) {
            Timber.w(e);
        }
        if (listResult != null) {
            Timber.d("list result: %s", listResult.getTotal());

            if (listResult.getList() != null) {
                mLastIndex = startIndex + listResult.getList().size();
                showImageList(listResult.getList(), isMore);
            }
        } else {
            showError("data error");
        }

        if (isMore) {
            if (getView() != null) {
                getView().showLoadingMore(false);
            }
        }
    }

    private void showImageList(List<SearchResult> results, boolean isMore) {
        List<ImageData> imageDataList = parseImageList(results);
        if (getView() != null) {
            if (!isMore)
                getView().showImage(imageDataList);
            else
                getView().showImageMore(imageDataList);
        }
    }

    private void showError(String error) {
        if (getView() != null) {
            getView().showError(error);
        }
    }

    private void onResponseError(Throwable e) {
        Timber.d("response error: %s", e.getMessage());
        if (getView() != null) {
            getView().showError(e.getMessage());
        }
    }

    private static List<ImageData> parseImageList(List<SearchResult> results) {
        List<ImageData> imageDataList = new ArrayList<>();
        for (SearchResult imageResult : results) {
            ImageData imageData = ImageData.parse(imageResult);
            if (imageData != null) {
                imageDataList.add(imageData);
            }
        }
        return imageDataList;
    }
}
